package com.zlzlib.manager.config;

import com.zlzlib.common.bean.BooleanEnum;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @DateTime: 2024/7/31 10:17
 * @Author zlz
 * @Version 1.0
 */
@MappedJdbcTypes({JdbcType.VARCHAR, JdbcType.CHAR})
@MappedTypes(BooleanEnum.class)
public class BooleanEnumTypeHandler extends BaseMyTypeHandler<BooleanEnum> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i,
                                    BooleanEnum parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter.getValue());
    }

    @Override
    protected BooleanEnum parseValue(String value) {
        try {
            return BooleanEnum.valueOf(value.toLowerCase());
        } catch (IllegalArgumentException e) {
            return BooleanEnum.no;
        }
    }
}
